package com.css.zfzx.fxpg.modules.buildings.census.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fxpg.common.utils.ServerUtil;
import com.css.zfzx.fxpg.modules.buildings.census.service.FxCensusBuildingService;
import com.css.zfzx.fxpg.modules.buildings.census.viewobjects.FxHouseQueryParams;
import com.css.zfzx.fxpg.modules.buildings.data.repository.entity.FxBuildingsEntity;
import com.css.zfzx.fxpg.modules.buildings.data.service.FxBuildingsService;
import com.css.zfzx.fxpg.modules.buildings.data.viewobjects.FxBuildingsQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author andy
 * @date 2020-11-11
 */
@Slf4j
@RestController
@RequestMapping("/fxCensusBuilding")
public class FxCensusBuildingController {
    @Autowired
    private FxCensusBuildingService fxCensusBuildingService;

    @GetMapping("/queryFxCensusBuildings")
    public RestResponse queryFxCensusBuildings(HttpServletRequest request, FxHouseQueryParams queryParams) {
        RestResponse response = null;
        try{
            String userId = ServerUtil.getUserId(request);
            String sort = request.getParameter("sort"), order = request.getParameter("order");
            if ("year".equals(sort)) sort = "buildTime";
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxCensusBuildingService.queryFxCensusHouses(queryParams,curPage,pageSize, sort, order, userId);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{houseType}/{id}")
    public RestResponse getFxCensusBuildings(@PathVariable String houseType, @PathVariable String id) {
        RestResponse response = null;
        try{
            FxBuildingsEntity fxBuildings = fxCensusBuildingService.getFxCensusBuildings(id, houseType);
            response = RestResponse.succeed(fxBuildings);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
/*
    @PostMapping
    public RestResponse saveFxBuildings(@RequestBody FxBuildingsEntity fxBuildings) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxBuildingsService.saveFxBuildings(fxBuildings);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateFxBuildings(@RequestBody FxBuildingsEntity fxBuildings)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxCensusBuildingService.updateFxBuildings(fxBuildings);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }*/

    @DeleteMapping
    public RestResponse deleteFxCensusBuildings(@RequestParam List<String> ids, @RequestParam String houseType) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxCensusBuildingService.deleteFxCensusBuildings(ids, houseType);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxCensusBuildingService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
/*
    @GetMapping("/export")
    public RestResponse exportExcel(FxBuildingsQueryParams queryParams, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = fxBuildingsService.exportExcel(queryParams, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        //return restResponse;
        return null;
    }

    @GetMapping("/getFxBuildingss")
    public RestResponse getFxBuildingss(HttpServletRequest request, FxBuildingsQueryParams queryParams) {
        RestResponse response = null;
        try{
            //JSONObject jsonObject = fxBuildingsService.getFxBuildingss(queryParams);
            //response = RestResponse.succeed(jsonObject);
            response = RestResponse.succeed(fxBuildingsService.getFxBuildingss(queryParams));
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
*/
    @PostMapping("/searchFxCensusBuildings")
    public RestResponse searchFxCensusBuildings(HttpServletRequest request, FxHouseQueryParams queryParams) {
        RestResponse response = null;
        try{
            String userId = ServerUtil.getUserId(request);
            JSONObject jsonObject = fxCensusBuildingService.searchFxCensusBuildings(queryParams, userId);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

}